﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication4.ServiceReference1;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication4
{
    public partial class AttackMap : Form
    {
        private Service1SoapClient world = new Service1SoapClient();
        private Graphics g;
        private Player[] players;
        private int marginal = 20;
        private int dist;
        private int biggest = 0;
        public Point Home { get; set; }
        private List<KeyValuePair<Village, GraphicsPath>> marks = new List<KeyValuePair<Village, GraphicsPath>>();
        public AttackMap()
        {
            InitializeComponent();
            dist = (int)numericUpDownDistance.Value;
            g = this.CreateGraphics();
            Home = new Point(58, 97);
            players = world.getPlayersInArea(58 - dist, 97 - dist, 58 + dist, 97 + dist);
            foreach (Player pl in players)
            {
                foreach (Village vil in pl.Villages)
                {
                    int size = (from h in vil.Histories
                                orderby h.Date descending
                                select h.Size).First();
                    if (size > biggest) biggest = size;
                }
            }
        }
        private bool drawMap() {
            labelInactive.Text = DateTime.Now.ToString();
            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            int scaleFactor = ((Height) - 2*marginal) / (dist * 2 + 1);            
marks = new List<KeyValuePair<Village,GraphicsPath>>();
            foreach (Player pl in players)
            {
                Pen mypen = new Pen(Color.FromArgb(255, 255, 0), 5);
                if (pl.Tribe == Race.Teutons)
                {
                    mypen.Color = Color.Red;
                }
                else if (pl.Tribe == Race.Romans)
                {
                    mypen.Color = Color.Blue;
                }
                else if (pl.Tribe == Race.Gallic)
                {
                    mypen.Color = Color.Green;
                }
                foreach (Village vil in pl.Villages)
                {
                    try
                    {
                        History first = (from h in vil.Histories
                                         orderby h.Date descending
                                         select h).First();
                        History second = (from h in vil.Histories
                                          where h.Size < first.Size
                                          orderby h.Date descending
                                          select h).Take(1).SingleOrDefault();
                        int size = first.Size;
                        TimeSpan inact = new TimeSpan(48, 0, 0);
                        if (second != null)
                        {                           
                             inact = first.Date - second.Date;
                            if (inact > new TimeSpan(48, 0, 0))
                            {
                                inact = new TimeSpan(48, 0, 0);
                            }
                        }
                         
                     int red = (int)(255.0 * (double)(inact.TotalMinutes / new TimeSpan(48,0,0).TotalMinutes));
                        int green = 255 - red;
                        Color myColor = Color.FromArgb(red, green, 0);
                        SolidBrush myBrush = new SolidBrush(myColor);  

                        size = (int)((double)scaleFactor * ((double)size / (double)biggest));
                        int x = (vil.X - (58 - dist)) * scaleFactor + marginal;
                        int y = (vil.Y - (97 - dist)) * scaleFactor + marginal;
                        Rectangle rect = new Rectangle(x, y, size, size);
                        GraphicsPath tmpGp = new GraphicsPath();
                        tmpGp.AddRectangle(rect);
                        
                        KeyValuePair<Village, GraphicsPath> tmp = new KeyValuePair<Village, GraphicsPath>(vil,tmpGp);
                        marks.Add(tmp);
                        g.DrawPath(mypen, tmpGp);
                                            

                        g.FillRectangle(myBrush, rect);

                    }
                    catch
                    {

                    }
                }
            }
            return true;
        }

        private void numericUpDownDistance_Paint(object sender, PaintEventArgs e)
        {
             
        }

        private void numericUpDownDistance_ValueChanged(object sender, EventArgs e)
        {
            dist = (int)numericUpDownDistance.Value;
            players = world.getPlayersInArea(Home.X - dist, Home.Y - dist, Home.X + dist, Home.Y + dist);
            drawMap();
        }
        private KeyValuePair<Village, GraphicsPath> old;
        private void AttackMap_MouseMove(object sender, MouseEventArgs e)
        {
            SolidBrush drawBrush = new SolidBrush(Color.Black);


            StringFormat drawFormat = new StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.NoWrap;
            Font drawFont = new Font("Arial", 10);

            KeyValuePair<Village, GraphicsPath> hit = (from gp in marks
                                where gp.Value.IsVisible(e.X, e.Y)
                                select gp).Take(1).SingleOrDefault();
            if (!old.Equals(hit)) {
                if (hit.Key != null)
                {
                    Player pl = (from p in this.players
                                 where p.ID == hit.Key.Owner
                                 select p).First();
                    int size = (from h in hit.Key.Histories
                                orderby h.Date descending
                                select h.Size).First();
                 label1.Text = "(" + hit.Key.X + "," + hit.Key.Y + ")";
                    labelVillage.Text = "Village: " + hit.Key.Name;
                    labelOwner.Text = "Player: " + pl.Name;
                    labelSize.Text = "Size: " + size.ToString() + " " + ((double)((double)size / (double)this.biggest)).ToString("F");
                    labelRace.Text = "Race: " + pl.Tribe.ToString();
                    labelDistance.Text = "Distance: " + Math.Sqrt(Math.Pow(hit.Key.X - Home.X,2.0) + Math.Pow(hit.Key.Y - Home.Y,2)).ToString("F");

                    if (hit.Key.Histories.Count() == 1)
                    {
                        labelInactive.Text = "Inactive: Always";
                    }
                    else
                    {
                        labelInactive.Text = "Inactive: " + (hit.Key.Histories.ElementAt(0).Date - hit.Key.Histories.ElementAt(1).Date).TotalHours.ToString("F")+ "h";
                    }
                    //Refresh();
                    //g.DrawString(hit.Key.Name + size, drawFont, drawBrush,marginal, this.Height - 50, drawFormat);
                }
            }
        }

        private void AttackMap_Paint(object sender, PaintEventArgs e)
        {           
            drawMap();          
        }

        private void AttackMap_Load(object sender, EventArgs e)
        {

        }

        private void AttackMap_ResizeEnd(object sender, EventArgs e)
        {
            this.Width = this.Height + 200;
        }
    }
}
